import 'dart:math';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:shopping_group/modle/main/entity/id_name_entity.dart';
import 'dart:js';
import 'object_util.dart';

class Utils {
  static String getImgPath(String name, {String format: 'png'}) {
    return 'images/$name.$format';
  }

  static String getPinyin(String str) {
    return PinyinHelper.getShortPinyin(str).substring(0, 1).toUpperCase();
  }

//  static Color getCircleBg(String str) {
//    String pinyin = getPinyin(str);
//    return getCircleAvatarBg(pinyin);
//  }

//  static Color getCircleAvatarBg(String key) {
//    return circleAvatarMap[key];
//  }

  static Color getChipBgColor(String name) {
    String pinyin = PinyinHelper.getFirstWordPinyin(name);
    pinyin = pinyin.substring(0, 1).toUpperCase();
    return nameToColor(pinyin);
  }

  static Color nameToColor(String name) {
    // assert(name.length > 1);
    final int hash = name.hashCode & 0xffff;
    final double hue = (360.0 * hash / (1 << 15)) % 360.0;
    return HSVColor.fromAHSV(1.0, hue, 0.4, 0.90).toColor();
  }

  static String reTextNull(String str) {
    if (null == str || str.isEmpty || '请选择' == str || 'null' == str) {
      return '';
    }
    return str;
  }

  static String reTextSelect(String str) {
    if (null == str || str.isEmpty || 'null' == str) {
      return '请选择';
    }
    return '请选择';
  }

  static String reTextNullZero(String str) {
    if (null == str || str.isEmpty || '请选择' == str || 'null' == str) {
      return '';
    }
    return '0';
  }

  static String reTimeNull(String str) {
    if (null == str || str.isEmpty || 'null' == str) {
      return '请选择';
    }
    return str;
  }

  static int reIntNull(String str) {
    if (null == str || str.isEmpty || '请选择' == str || 'null' == str) {
      return 0;
    }
    return int.parse(str);
  }

  static bool showNullToast(BuildContext context, String str, String toastStr) {
    if (null == str || str.isEmpty || '请选择' == (str)) {
      StringBuffer sb = new StringBuffer();
      if ('请选择' == (str)) {
        sb..write('请选择')..write(toastStr);
      } else {
        sb..write(toastStr)..write('未填写');
      }
      BotToast.showText(text: sb.toString());
      return true;
    }
    return false;
  }

  static String getSpinnerId(List list, String name) {
    for (var item in list) {
      if (item.name == name) {
        return item.id;
      }
    }
    return '';
  }

  static String getSpinnerName(List list, String id) {
    for (var item in list) {
      if (item.id == id || item.name == id) {
        return item.name;
      }
    }
    return '请选择';
  }

  //根据秒获取时间格式   结果为 ：  long i =  2591881;   29天23小时58分钟1秒
  static String formatDateTime(String mssDay) {
    double mss = ObjectUtil.isEmptyString(mssDay) ? '0' : double.parse(mssDay);
    StringBuffer dateTimes = StringBuffer();
    double days = (mss / (60 * 60 * 24));
    double hours = ((mss % (60 * 60 * 24)) / (60 * 60));
    double minutes = ((mss % (60 * 60)) / 60);
    double seconds = mss % 60;
    dateTimes.write('');
    if (days > 0) {
      dateTimes
        ..write(days.round())
        ..write('天')
        ..write(hours.round())
        ..write('小时')
        ..write(minutes.round())
        ..write('分钟')
        ..write(seconds)
        ..write('秒');
    } else if (hours > 0) {
      dateTimes
        ..write(hours.round())
        ..write('小时')
        ..write(minutes.round())
        ..write('分钟')
        ..write(seconds.round())
        ..write('秒');
    } else if (minutes > 0) {
      dateTimes
        ..write(minutes.round())
        ..write('分钟')
        ..write(seconds.round())
        ..write('秒');
    } else {
      dateTimes..write(seconds.round())..write('秒');
    }

    return dateTimes.toString();
  }

  static Map<String, IdNameEntity> toMap(List<IdNameEntity> list) {
    var map = Map<String, IdNameEntity>.fromIterable(
      list,
      key: (element) => element.id,
      value: (element) => element,
    );
    return map;
  }

  static String formatNum(double num, int postion) {
    if ((num.toString().length - num.toString().lastIndexOf(".") - 1) <
        postion) {
      //小数点后有几位小数
      return (num.toStringAsFixed(postion)
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString());
    } else {
      return (num.toString()
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString());
    }
  }

  static String getUUid() {
    String randomstr = Random().nextInt(10).toString();
    for (var i = 0; i < 3; i++) {
      var str = Random().nextInt(10);
      randomstr = "$randomstr" + "$str";
    }
    var timenumber = DateTime.now().millisecondsSinceEpoch; //时间
    var uuid = "$randomstr" + "$timenumber";
    return uuid.toString();
  }

  static int getRandomInt(int min, int max) {
    final _random = new Random();
//定义 指定返回值为 int 的函数方法,传入参数为 min 和 max
//将 参数min + 取随机数（最大值范围：参数max -  参数min）的结果 赋值给变量 result;
    var result = min + _random.nextInt(max - min);
//返回变量 result 的值;
    return result;
  }

  static bool isWeixin() {
    var iswx = context.callMethod("isWeixin");
    // js.context.callMethod('flutterAlert', ['iswx:' + iswx]);
    if (null != iswx && iswx is bool) {
      return iswx;
    }
    return false;
  }
}
